package com.yw.swagger.service.serviceImpl;


import com.yw.swagger.mapper.SpNoticeMapper;
import com.yw.swagger.mapper.SysBpsGpmemlnkMapper;
import com.yw.swagger.mapper.SysBpsGroupsMapper;
import com.yw.swagger.model.SysBpsGpmemlnk;
import com.yw.swagger.model.Utree;
import com.yw.swagger.service.SysBpsGroupsService;
import com.yw.swagger.tool.Ztree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SysBpsGroupsServiceImpl implements SysBpsGroupsService {
    @Autowired
    SysBpsGroupsMapper sysBpsGroupsMapper;
    @Autowired
    private SpNoticeMapper spNoticeMapper;
    @Autowired
    private SysBpsGpmemlnkMapper sysBpsGpmemlnkMapper;

    @Override
    public  List<Ztree> selectSysBps() {
        List<Map<String, Object>>maps= spNoticeMapper.selectSysBps();
        List<Ztree> ztrees = new ArrayList<Ztree>();
        List<Map<String, Object>>map=new ArrayList<>();
        List<Map<String, Object>>map1=new ArrayList<>();
        for(Map m:maps){
            int GroupId=(int) m.get("GROUP_ID");
            map= spNoticeMapper.selectSysBpsG(GroupId);
            for(Map s:map){
                map1.add(s);
            }
        }
        for(Map m:maps){
            String GroupName=(String) m.get("GROUP_Name");
            int GroupId=(int) m.get("GROUP_ID");
            int pid=(int) m.get("GROUP_PARENTID");
            Ztree ztree = new Ztree();
            if(pid==0){
                ztree.setId(Long.valueOf(pid));
                ztree.setpId(1L);
                ztree.setName(GroupName);
                ztree.setTitle(GroupName);
            }else {
                ztree.setId(Long.valueOf(GroupId));
                ztree.setpId(0L);
                ztree.setName(GroupName);
                ztree.setTitle(GroupName);
            }
            ztrees.add(ztree);
        }
        for(Map m:map1){
            String GroupName=(String) m.get("GROUP_Name");
            int GroupId=(int) m.get("GROUP_ID");
            String GPMEMUser=(String) m.get("GPMEM_User");
            Ztree ztree = new Ztree();
            ztree.setId(2L);
            ztree.setpId(Long.valueOf(GroupId));
            ztree.setName(GroupName);
            ztree.setTitle(GPMEMUser);
            ztrees.add(ztree);
        }
        return ztrees ;
    }
    //查询局领导人员
    @Override
    public List<SysBpsGpmemlnk> selectbureauPerson() {
        return sysBpsGpmemlnkMapper.selectbureauPerson();
    }
    //查询办公室人员
    @Override
    public List<SysBpsGpmemlnk> selectworkPerson() {
        return sysBpsGpmemlnkMapper.selectworkPerson();
    }

    List<Utree>  getChildItem(List<Utree> list, Long prentId){
        List<Utree> temps=new ArrayList<>();
        System.err.println(111111);
        for(Utree item:list){
            if(item.getpId().equals(prentId)){
                System.err.println("pid:"+item.getpId());
                System.err.println("id:"+item.getId());
                item.setList(getChildItem(list, item.getId()));
                System.err.println("item:"+item.getList());
                temps.add(item);
                System.err.println("aaaa");
            }
        }
        System.err.println("2222");
        return  temps;
    }
}
